//
//  LendTableViewCell.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/29.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LendTableViewCell.h"

@interface LendTableViewCell()
@property (nonatomic, strong)UILabel * lendRateLab;//出借率
@property (nonatomic, strong)UILabel * lendTitleLab;//出借标题
@property (nonatomic, strong)UILabel * projectLab;//项目名称
@property (nonatomic, strong)UILabel * timeLimitsLabTitle;//期限标题
@property (nonatomic, strong)UILabel * timeLimitsContentLab;//期限天数
@property (nonatomic, strong)UILabel * investmentVolumeLabTitle;//投资额
@property (nonatomic, strong)UILabel * investmentVolumeContentLab;//投资具体额
@property (nonatomic, strong)UIButton * joinBtn;//立即加入按钮
@property (nonatomic, strong)NSString * bidID;//标的ID
@end

@implementation LendTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        UIView * bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = getColor(grayColor);
        [self.contentView addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.contentView);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        [self.contentView addSubview:self.lendRateLab];
        [self.contentView addSubview:self.lendTitleLab];
        [self.contentView addSubview:self.projectLab];
        [self.contentView addSubview:self.timeLimitsLabTitle];
        [self.contentView addSubview:self.timeLimitsContentLab];
        [self.contentView addSubview:self.investmentVolumeLabTitle];
        [self.contentView addSubview:self.investmentVolumeContentLab];
        [self.contentView addSubview:self.joinBtn];
        [self.projectLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(24/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(19/WIDTH_6_SCALE);
            make.height.mas_offset(12/WIDTH_6_SCALE);
            make.width.mas_offset(250/WIDTH_6_SCALE);
        }];
        [self.lendRateLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(33/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.projectLab.mas_bottom).mas_offset(15/WIDTH_6_SCALE);
            make.width.mas_offset(75/WIDTH_6_SCALE);
            make.height.mas_offset(20/WIDTH_6_SCALE);
        }];
        [self.lendTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(20/WIDTH_6_SCALE);
            make.height.mas_offset(11/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.lendRateLab.mas_bottom).mas_offset(8/WIDTH_6_SCALE);
        }];
        [self.timeLimitsLabTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.lendTitleLab.mas_right).mas_offset(30/WIDTH_6_SCALE);
            make.top.height.equalTo(weakSelf.lendTitleLab);
        }];
        [self.timeLimitsContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(15/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.lendRateLab.mas_top).mas_offset(5/WIDTH_6_SCALE);
            make.centerX.equalTo(weakSelf.timeLimitsLabTitle.mas_centerX);
        }];
        [self.investmentVolumeLabTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.timeLimitsLabTitle.mas_right).mas_offset(30/WIDTH_6_SCALE);
            make.top.height.equalTo(weakSelf.timeLimitsLabTitle);
        }];
        [self.investmentVolumeContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.top.equalTo(weakSelf.timeLimitsContentLab);
            make.centerX.equalTo(weakSelf.investmentVolumeLabTitle.mas_centerX);
        }];
        [self.joinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.projectLab.mas_top);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-18/WIDTH_6_SCALE);
            make.height.mas_offset(28/WIDTH_6_SCALE);
            make.width.mas_offset(64/WIDTH_6_SCALE);
        }];
        
    }return self;
}
#pragma mark - init
- (UILabel *)lendRateLab{
    if (!_lendRateLab) {
        _lendRateLab = [[UILabel alloc] init];
        _lendRateLab.font = DEF_FontSize_10;
        _lendRateLab.textColor = [UIColor orangeColor];
        _lendRateLab.textAlignment = NSTextAlignmentLeft;
//        //改变字体
//        NSString * lendRateStr = @"10.0%";
//        NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:lendRateStr];
//        // 需要改变的第一个文字的位置
//        NSUInteger firstLoc = 0;
//        // 需要改变的最后一个文字的位置
//        NSUInteger secondLoc = [[noteStr string] rangeOfString:@"%"].location;
//        // 需要改变的区间
//        NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
//        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
//        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
//        [_lendRateLab setAttributedText:noteStr];
    }return _lendRateLab;
}

- (UILabel *)lendTitleLab{
    if (!_lendTitleLab) {
        _lendTitleLab = [[UILabel alloc] init];
        _lendTitleLab.font = DEF_FontSize_10;
        _lendTitleLab.textColor = getColor(@"959595");
        _lendTitleLab.textAlignment = NSTextAlignmentLeft;
        _lendTitleLab.text = @"平台建议年均出借率";
    }return _lendTitleLab;
}

- (UILabel *)projectLab{
    if (!_projectLab) {
        _projectLab = [[UILabel alloc] init];
        _projectLab.font = DEF_FontSize_12;
        _projectLab.textColor = getColor(@"737373");
        _projectLab.textAlignment = NSTextAlignmentLeft;
//        _projectLab.text = @"【家家泰365天J16062141】";
    }return _projectLab;
}

- (UILabel *)timeLimitsLabTitle{
    if (!_timeLimitsLabTitle) {
        _timeLimitsLabTitle = [[UILabel alloc] init];
        _timeLimitsLabTitle.font = DEF_FontSize_10;
        _timeLimitsLabTitle.textColor = getColor(@"959595");
        _timeLimitsLabTitle.textAlignment = NSTextAlignmentLeft;
        _timeLimitsLabTitle.text = @"投标期限";
    }return _timeLimitsLabTitle;
}

- (UILabel *)timeLimitsContentLab{
    if (!_timeLimitsContentLab) {
        _timeLimitsContentLab = [[UILabel alloc] init];
        _timeLimitsContentLab.font = DEF_FontSize_10;
        _timeLimitsContentLab.textColor = getColor(@"959595");
        _timeLimitsContentLab.textAlignment = NSTextAlignmentLeft;
//        _timeLimitsContentLab.text = @"365天";
    }return _timeLimitsContentLab;
}

- (UILabel *)investmentVolumeContentLab{
    if (!_investmentVolumeContentLab) {
        _investmentVolumeContentLab = [[UILabel alloc] init];
        _investmentVolumeContentLab.font = DEF_FontSize_10;
        _investmentVolumeContentLab.textColor = getColor(@"959595");
        _investmentVolumeContentLab.textAlignment = NSTextAlignmentLeft;
//        _investmentVolumeContentLab.text = @"1万元";
    }return _investmentVolumeContentLab;
}

- (UILabel *)investmentVolumeLabTitle{
    if (!_investmentVolumeLabTitle) {
        _investmentVolumeLabTitle = [[UILabel alloc] init];
        _investmentVolumeLabTitle.font = DEF_FontSize_10;
        _investmentVolumeLabTitle.textColor = getColor(@"959595");
        _investmentVolumeLabTitle.textAlignment = NSTextAlignmentLeft;
        _investmentVolumeLabTitle.text = @"剩余投标额";
    }return _investmentVolumeLabTitle;
}

-  (UIButton *)joinBtn{
    if (!_joinBtn) {
        _joinBtn = [[UIButton alloc] init];
        _joinBtn.titleLabel.font = DEF_FontSize_10;
        [_joinBtn addTarget:self action:@selector(joinBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_joinBtn setTitle:@"立即加入" forState:UIControlStateNormal];
        [_joinBtn setBackgroundColor:getColor(@"ff8819")];
        [_joinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _joinBtn.layer.cornerRadius = 4;
        _joinBtn.layer.masksToBounds = YES;
    }return _joinBtn;
}
#pragma mark - 方法
//首字母就是数字的
- (void)changeStringTypeface:(NSString *)string withFirstLoc:(NSInteger)firstLoc withSecondLocStr:(NSString *)secondLocStr withLabel:(UILabel *)label{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
    //     需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:secondLocStr].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [label setAttributedText:noteStr];
}

//首字母是字母的
- (void)changeStringTypeface:(NSString *)string withFirstLocStr:(NSString *)str withSecondLocStr:(NSString *)secondLocStr withLabel:(UILabel *)label{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:str].location + 1;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:secondLocStr].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [label setAttributedText:noteStr];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    //标的名称
    self.projectLab.text = [NSString stringWithFormat:@"【%@】",model[@"entitle"]];
    //投标期限
    self.timeLimitsContentLab.text = [NSString stringWithFormat:@"%@天",model[@"invtime"]];
    //剩余投标额
    self.investmentVolumeContentLab.text = [NSString stringWithFormat:@"%@万元",model[@"sursum"]];
    //标的ID
    self.bidID = model[@"captionid"];
    //出借率
    //改变字体
    NSString * lendRateStr = model[@"lolimit"];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:lendRateStr];
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = 0;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:@"%"].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    [self.lendRateLab setAttributedText:noteStr];
    if ([model[@"bstate"] isEqualToString:@"1"]) {
//        灰色
        [self.joinBtn setBackgroundColor:[UIColor clearColor]];
        [self.joinBtn setTitle:@"还款中" forState:UIControlStateNormal];
        [self.joinBtn setTitleColor:getColor(@"bdbdbd") forState:UIControlStateNormal];
        self.joinBtn.userInteractionEnabled = NO;
    }
    if ([model[@"bstate"] isEqualToString:@"2"]) {
        [self.joinBtn setBackgroundColor:[UIColor clearColor]];
        [self.joinBtn setTitle:@"已结束" forState:UIControlStateNormal];
        [self.joinBtn setTitleColor:getColor(@"bdbdbd") forState:UIControlStateNormal];
        self.joinBtn.userInteractionEnabled = NO;
    }
    if ([model[@"bstate"] isEqualToString:@"3"]) {
        [self.joinBtn setTitle:@"立即加入" forState:UIControlStateNormal];
        [self.joinBtn setBackgroundColor:getColor(@"ff8819")];
        [self.joinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.joinBtn.userInteractionEnabled = YES;
    }
}
#pragma mark - action
- (void)joinBtnClick:(UIButton *)sender{
    NSLog(@"立即加入");
    if (self.siftPageBlock) {
        self.siftPageBlock(self.bidID);
    }
}
@end
